home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / computerjanitor / plugins / unsupported_plugin.py < prev    next >
Encoding:
Python Source  |  2009-04-17  |  4.3 KB  |  120 lines

  1. # unsupported_plugin.py - remove packages no longer supported by Canonical
  2. # Copyright (C) 2008  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import os
  18.  
  19. import computerjanitor
  20. import computerjanitorapp
  21. _ = computerjanitorapp.setup_gettext()
  22.  
  23.  
  24. class UnsupportedPackagesPlugin(computerjanitor.Plugin):
  25.  
  26.     """Plugin to find packages no longer supported by Canonical.
  27.     
  28.     An unsupported package is one that is no longer available in the
  29.     archive.
  30.     
  31.     Unfortunately, this heuristic is unable to treat packages installed
  32.     manually (via dpkg), or from sources (such as PPAs) that are no longer
  33.     in sources.list.
  34.  
  35.     """
  36.     
  37.     description = _("Package is no longer supported: it is no longer in the "
  38.                     "package archive. (It may also have been installed from "
  39.                     "an unofficial archive that is no longer available. In "
  40.                     "that case you may want to keep it.)")
  41.  
  42.     basenames = ["linux-image", "linux-headers", "linux-image-debug",
  43.                  "linux-ubuntu-modules", "linux-header-lum",
  44.                  "linux-backport-modules",
  45.                  "linux-header-lbm", "linux-restricted-modules"]
  46.  
  47.     def __init__(self):
  48.         self.uname = os.uname()[2]
  49.  
  50.     def is_current_kernel(self, pkg):
  51.         """Is pkg the currently running kernel or a related package?
  52.  
  53.         We don't want to remove the currently running kernel. The
  54.         kernel packages have names that start with the strings in
  55.         self.basenames, and end with the 'release' string from
  56.         os.uname.
  57.  
  58.         """
  59.  
  60.         # FIXME: This code is based on that in update-manager. It
  61.         # should be shared, but that's impractical to arrange in an
  62.         # SRU.
  63.  
  64.         for base in self.basenames:
  65.             if pkg.name == "%s-%s" % (base, self.uname):
  66.                 return True
  67.                                  
  68.         return False
  69.  
  70.     def installed_deps(self, pkg): # pragma: no cover
  71.         """Return set of names of installed dependencies of a package.
  72.  
  73.         Only immediate dependencies, dependency graph is not traversed
  74.         deeply.
  75.  
  76.         """
  77.  
  78.         result = set()
  79.         for dep in pkg.installedDependencies:
  80.             for or_dep in dep.or_dependencies:
  81.                 result.add(or_dep.name)
  82.         return result
  83.     
  84.     def has_installed_rdepends(self, cache, pkg): # pragma: no cover
  85.         """Does a package have any installed reverse dependencies?"""
  86.         installed = [x for x in cache if x.isInstalled]
  87.         for x in installed:
  88.             if pkg.name in self.installed_deps(x):
  89.                 return True
  90.         return False
  91.  
  92.     def is_supported(self, pkg):
  93.         """Is a package supported?"""
  94.  
  95.         # FIXME: we do not have a way right now to tell if a package
  96.         #        got manually installed or comes from a source (e.g. a PPA)
  97.         #        that was once part of the system and later got disabled
  98.  
  99.         if not pkg.isInstalled:
  100.             return True
  101.         if pkg.installedDownloadable or pkg.candidateDownloadable:
  102.             return True
  103.         if len(pkg._pkg.VersionList) > 1:
  104.             # if there is just a single version and that is not
  105.             # downloadable anymore, then its cruft this avoids cases
  106.             # where the user has e.g. a newer version installed from a
  107.             # ppa that is no longer downloadable, but the main archive
  108.             # has the version still
  109.             return True
  110.         if self.is_current_kernel(pkg):
  111.             # The currently running kernel is always supported. So there.
  112.             return True
  113.         return False
  114.  
  115.     def get_cruft(self):
  116.         for pkg in self.app.apt_cache:
  117.             if not self.is_supported(pkg):
  118.                 if not self.has_installed_rdepends(self.app.apt_cache, pkg):
  119.                      yield computerjanitor.PackageCruft(pkg, self.description)
  120.